package com.smartcommunity.controller.village;
import com.smartcommunity.bean.constant.Constant;
import com.smartcommunity.bean.enumeration.ResultCode;
import com.smartcommunity.bean.model.Result;
import com.smartcommunity.controller.BaseController;
import com.smartcommunity.entity.house.HouseListUnitResp;
import com.smartcommunity.entity.village.House;
import com.smartcommunity.entity.village.HouseListReq;
import com.smartcommunity.entity.village.dto.HouseTypeEditBatch;
import com.smartcommunity.entity.village.dto.PeopleInHouse;
import com.smartcommunity.log.interceptor.Log;
import com.smartcommunity.service.village.impl.HouseService;
import com.smartcommunity.utils.StringUtils;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: HouseController
 * @Description: 实有房屋控制层
 * @author: minsheng
 * @Date: 2019/1/17 15:01
 * @Company:
 * @version V1.5.0
 */
@RestController
@RequestMapping("house")
@Api(value = "小区服务-实有房屋相关",tags = "实有房屋")
@Slf4j
public class HouseController extends BaseController {

    @Autowired
    private HouseService service;

    @PostMapping("list")
    @ApiOperation(value = "查询房屋",notes = "查询房屋")
    @Log(info = "查询房屋列表信息",module = "房屋管理",function = "查询")
    public Result list(@RequestBody HouseListReq houseListReq){
        List<HouseListUnitResp> houses = service.list(houseListReq);
        return Result.ok(houses);
    }

    /**
     * @Description: 新增房屋
     * @param: [list]
     * @return: java.util.List
     * @author: zdy
     * @date: 2020/1/9
     */
    @PostMapping("insertHouse")
    @ApiOperation(value = "新增房屋",notes = "新增房屋")
    @Log(info = "新增房屋信息",module = "房屋管理",function = "新增")
    public Result insertHouse(@Valid @RequestBody @ApiParam(name = "house", value = "房屋对象") House house){
        log.info("接口：addHouse；新增房屋");
        return service.addHouse(house);
    }
    /**
     * @Description: 修改房屋
     * @param: [list]
     * @return: java.util.List
     * @author: minsheng
     * @date: 2019/1/17 15:05
     */
    @PostMapping("updateHouse")
    @ApiOperation(value = "修改房屋",notes = "修改房屋")
    @Log(info = "修改房屋信息",module = "房屋管理",function = "修改")
    public Result updateHouse(@Valid @RequestBody @ApiParam(name = "house", value = "房屋对象") House house){
        log.info("接口：updateHouse；修改房屋");
        return service.updateHouse(house);
    }
    /**
     * @Description: 删除房屋
     * @param: [list]
     * @return: java.util.List
     * @author: zdy
     * @date: 2019/1/17 15:05
     */
    @DeleteMapping("deleteHouse")
    @ApiOperation(value = "删除房屋",notes = "删除房屋")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" ,value = "房屋ID，多个以逗号分隔")
    })
    @Log(info = "删除房屋信息",module = "房屋管理",function = "删除")
    public Result deleteHouse(@RequestParam(required = true, name = "id") String id){
        log.info("接口：deleteHouse；删除房屋");
        return service.deleteHouse(id);
    }


    /**
     * @Description: 更新人房关系
     * @param: [list]
     * @return: java.util.List
     * @author: minsheng
     * @date: 2019/1/17 15:05
     */
    @PostMapping("updatePeopleHouse")
    @ApiOperation(value = "新增,更新人房关系",notes = "新增,更新人房关系")
    @Log(info = "更新人房关系",module = "房屋管理",function = "更新")
    public Result updatePeopleHouse(@RequestBody List<PeopleInHouse> list){
        log.info("接口：deleteHouse；更新人房关系");
        return service.insertPeopleHouse(list);
    }

    /**
     * @Description: 删除人房关系
     * @param: [list]
     * @return: java.util.List
     * @author: minsheng
     * @date: 2019/1/17 15:05
     */
    @PostMapping("deletePeopleHouse")
    @ApiOperation(value = "删除人房关系",notes = "删除人房关系")
    @Log(info = "删除人房关系",module = "房屋管理",function = "删除")
    public Result deletePeopleHouse(@RequestBody List<PeopleInHouse> list){
        log.info("接口：deleteHouse；删除人房关系");
        return service.deletePeopleHouse(list);
    }
    /**
     * @Description: 查询房屋人员列表
     * @param: [villageCode, houseNo, buildingNo]
     * @return: java.util.MaplistAllPeoples
     * @author: minsheng
     * @date: 2019/1/17 15:08
     */
    @GetMapping("listPeoplesInHouse")
    @ApiOperation(value = "查询房屋人员列表",notes = "查询房屋人员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "villageCode" ,required = true,value = "小区编号"),
            @ApiImplicitParam(name = "houseNo" ,required = true,value = "房屋编号"),
            @ApiImplicitParam(name = "unitNo" ,required = true,value = "单元号"),
            @ApiImplicitParam(name = "buildingNo" ,required = true,value = "楼栋编号")
    })
    @Log(info = "查询房屋人员列表",module = "房屋管理",function = "查询")
    public Result listPeoplesInHouse(@RequestParam(name = "villageCode") String villageCode,
                                  @RequestParam(name = "houseNo") String houseNo,
                                  @RequestParam(name = "unitNo") String unitNo,
                                  @RequestParam(name = "buildingNo") String buildingNo) {
        log.info("接口：relPeopleList；查询房屋人人员列表");
        Map map = new HashMap(4);
        map.put("villageCode", villageCode);
        map.put("houseNo", houseNo);
        map.put("unitNo", unitNo);
        map.put("buildingNo", buildingNo);
        return Result.ok(service.listPeople(map));
    }

    @GetMapping("countHouseByType")
    @ApiOperation(value = "按类型统计房屋",notes = "按类型统计房屋")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "villageCode", value = "小区编号"),
    })
    @Log(info = "查询实有房屋分类统计信息",module = "房屋管理",function = "查询")
    public Result countHouseByType(@RequestParam(required = false,name = "villageCode") String villageCode){
        if( StringUtils.isEmpty(villageCode)){
            return Result.ok();
        }
        Map<String,Object> map = new HashMap(2);
        map.put("villageCode",villageCode);
        map.put("type", Constant.HOUSETYPE);
        return service.countHouseByType(map);
    }
    
    @ApiOperation(value = "房屋-数据迁移到ES-basedata索引")
    @GetMapping(value = "/syncDataToEs")
    public Result syncDataToEs() {
        try {
			return service.syncDataToEs();
		} catch (Exception e) {
			log.error("实有房屋历史数据迁移到ES-basedata索引异常:", e);
			return Result.error(ResultCode.ERROR);
		}
    }

    @PostMapping("houseTypeEditBatch")
    @ApiOperation(value = "批量修改房屋类型",notes = "批量修改房屋类型")
    @Log(info = "批量修改房屋类型",module = "房屋管理",function = "修改")
    public Result houseTypeEditBatch(@RequestBody HouseTypeEditBatch houseTypeEditBatches){
        service.houseTypeEditBatch(houseTypeEditBatches);
        return Result.ok();
    }
}
